﻿// Nail framework
//
// Copyright © Frank Nagl, 2008-2009
// admin@franknagl.de
// www.franknagl.de
//
namespace Nail
{
    using System;

    /// <summary>A stopwatch to stop timespans.</summary>
    public static class Stopwatch
    {
        private static System.Diagnostics.Stopwatch Watch;

        /// <summary>
        /// Starts the stopwatch.
        /// </summary>
        public static void Start()
        {
            Watch = new System.Diagnostics.Stopwatch();
            Watch.Start();
        }

        /// <summary>
        /// Ends the stopwatch. Shows the result time.
        /// </summary>
        /// <param name="messageBox">if set to <c>true</c> the 
        /// result time is  shown in a message box, otherwise in the console.</param>
        public static void End(bool messageBox)
        {
            // Stop stopwatch
            Watch.Stop();

            // Set elapsed time
            TimeSpan end = Watch.Elapsed;
            string s = "Time: " + end.Seconds + "."
                                + end.Milliseconds + "."
                                + end.Ticks + "s";
            if (messageBox)
                System.Windows.Forms.MessageBox.Show(s, "Nail's Stopwatch");
            else
                Console.WriteLine(s);

            // Bring stopwatch back to zero
            Watch.Reset();
        }
    }
}
